package com.cyl.manager.act.service;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.act.domain.FhMapWatercommonfish1;
import com.cyl.manager.act.domain.FhMapFishtype1;
import com.cyl.manager.act.domain.dto.FhMapWaterD;
import com.cyl.manager.act.mapper.FhMapWaterDTOMapper;
import com.cyl.manager.oms.domain.entity.FhUserInfo;
import com.cyl.manager.oms.domain.entity.Info1User;
import com.cyl.manager.oms.mapper.FhUserInfoMapper;
import com.cyl.manager.oms.mapper.User1InfoMapper;
import com.cyl.manager.ums.domain.entity.UserInfo;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.config.LocalDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.cyl.manager.act.mapper.FhMypunctuationMapper;
import com.cyl.manager.act.domain.entity.FhMypunctuation;
import com.cyl.manager.act.domain.query.FhMypunctuationQuery;

/**
 * 我的标点Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class FhMypunctuationService {
    @Autowired
    private FhMypunctuationMapper fhMypunctuationMapper;

    @Autowired
    private FhMapWaterDTOMapper fhMapWaterMapper;

    @Autowired
    private FhMypunctuationService fhMypunctuationService;

    @Autowired
    private User1InfoMapper userInfoMapper;

    /**
     * 查询我的标点
     *
     * @param id 我的标点主键
     * @return 我的标点
     */
    public FhMypunctuation selectById(Integer id) {
        return fhMypunctuationMapper.selectById(id);
    }

    /**
     * 查询我的标点列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 我的标点
     */
    public List<FhMypunctuation> selectList(FhMypunctuationQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<FhMypunctuation> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        String nameLike = query.getNameLike();
        if (!StringUtils.isEmpty(nameLike)) {
            qw.like("name", nameLike);
        }
        String longitude = query.getLongitude();
        if (!StringUtils.isEmpty(longitude)) {
            qw.eq("longitude", longitude);
        }
        String latitude = query.getLatitude();
        if (!StringUtils.isEmpty(latitude)) {
            qw.eq("latitude", latitude);
        }
        LocalDateTime crateTime = query.getCrateTime();
        if (crateTime != null) {
            qw.eq("crate_time", crateTime);
        }
        String address = query.getAddress();
        if (!StringUtils.isEmpty(address)) {
            qw.eq("address", address);
        }
        Integer userId = query.getUserId();
        if (userId != null) {
            qw.eq("user_id", userId);
        }
        qw.orderByDesc("crate_time");
        return fhMypunctuationMapper.selectList(qw);
    }

    /**
     * 新增我的标点
     *
     * @param fhMypunctuation 我的标点
     * @return 结果
     */
    public int insert(FhMypunctuation fhMypunctuation) {
        //后端获取用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        fhMypunctuation.setUserId(loginUser.getUser().getUserId().intValue());
        fhMypunctuation.setCrateTime(LocalDateTime.now());
        return fhMypunctuationMapper.insert(fhMypunctuation);
    }

    /**
     * 修改我的标点
     *
     * @param fhMypunctuation 我的标点
     * @return 结果
     */
    public int update(FhMypunctuation fhMypunctuation) {
        return fhMypunctuationMapper.updateById(fhMypunctuation);
    }

    /**
     * 批量删除我的标点
     *
     * @param ids 需要删除的我的标点主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return fhMypunctuationMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除我的标点信息
     *
     * @param id 我的标点主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return fhMypunctuationMapper.updateDelFlagByIds(ids);
    }


    /*
     *  审核
     * */
    public int audit(FhMypunctuation fhMypunctuation) {

        if (fhMypunctuation.getStatus() == 1){
            FhMapWaterD fhMapWater = new FhMapWaterD();
            FhMapWaterD fhMapWaterv = fhMapWaterMapper.selectFhMapWaterByjfd(fhMypunctuation.getLongitude(), fhMypunctuation.getLatitude());
            if (fhMapWaterv != null){
                if (fhMapWaterv.getId() != null) {
                    fhMapWater.setId(Long.valueOf(fhMapWaterv.getId()));
                }
                fhMapWater.setWaterName(fhMapWaterv.getWaterName());
            }else{
                fhMapWater.setWaterName(fhMypunctuation.getName());
                fhMapWater.setWaterType("2");
                fhMapWater.setLocationName(fhMypunctuation.getAddress());
                fhMapWater.setLongitude(fhMypunctuation.getLongitude());
                fhMapWater.setLatitude(fhMypunctuation.getLatitude());
                fhMapWater.setFishids(fhMypunctuation.getFishIds());
                fhMapWaterMapper.insertFhMapWater(fhMapWater);
            }
            //先得到上面insert的id
            Long id = fhMapWater.getId();

            if (fhMypunctuation.getJifen() != null && fhMypunctuation.getJifen() > 0) {
                Long userId = fhMypunctuation.getUserId().longValue();
                // 查询用户积分信息，只查询需要的字段
                QueryWrapper<Info1User> userQueryWrapper = new QueryWrapper<>();
                userQueryWrapper.select("id", "user_id", "points"); // 指定只查询这些字段
                userQueryWrapper.eq("user_id", userId);
                Info1User userInfo = userInfoMapper.selectOne(userQueryWrapper);
                if (userInfo != null) {
                    userInfo.setPoints((userInfo.getPoints() == null ? 0 : userInfo.getPoints()) + fhMypunctuation.getJifen());
                    userInfoMapper.updateById(userInfo);
                }
            }


            String fishIds = fhMypunctuation.getFishIds();
            if (fishIds != null && !fishIds.isEmpty()) {
                String[] fishIdArr = fishIds.split(",");
                for (String fishId : fishIdArr) {
                    if (fishId != null && !fishId.isEmpty()) {
                    Integer fhMapWatercommonfish1 = fhMapWaterMapper.selectFhMapWatercommonfishById(id, Long.valueOf(fishId));
                    if (fhMapWatercommonfish1 == null) {
                        FhMapWatercommonfish1 fhMapWatercommonfish = new FhMapWatercommonfish1();
                        fhMapWatercommonfish.setWaterId(id);
                    fhMapWatercommonfish.setFishId(Long.valueOf(fishId));
                    fhMapWatercommonfish.setWaterName(fhMapWater.getWaterName());
                    fhMapWatercommonfish.setCatchNum(0L);
                    FhMapFishtype1 fhMapFishtype = fhMapWaterMapper.selectFhMapFishtypeById(Long.valueOf(fishId));
                    fhMapWatercommonfish.setFishName(fhMapFishtype.getFishType());
                    fhMapWatercommonfish.setFishImage(fhMapFishtype.getFishImage());
                    fhMapWatercommonfish.setStatus("0");
                    fhMapWaterMapper.insertFhMapWatercommonfish(fhMapWatercommonfish);
                    }
                }
            }
            }
        }

        return fhMypunctuationMapper.updateById(fhMypunctuation);
    }

    public Long contSign(Long userId) {
        return fhMypunctuationMapper.contSign(userId);
    }
}
